import { Component } from '@angular/core';
import {Router, ActivatedRoute, Params, Data} from '@angular/router';
import 'rxjs/add/operator/switchMap';
import { HomeService } from '../../service/home.service';
import { isBindPNService } from '../../service/isBindPN.service';
import { userCenterService } from '../../service/userCenter.service';
import { Pagination } from  '../../lib/pagination';

/* wiper插件 */
import 'swiper';
import {ajaxGetJSON} from "rxjs/observable/dom/AjaxObservable";
declare let $:any;
declare let Swiper:any;

@Component({
    selector: 'home',
    templateUrl: '../../../views/home/home.html',
    styleUrls: [ '../../../css/home.css'],
    providers: [
        Pagination,
        isBindPNService,
        HomeService,
        userCenterService
    ]
})
export  class HomeComponent {
    public games : any = {};
    public games1 : any = [];
    public resource_url:any;
    public isPayGame:any = true;
    // private id : number; // 微信公众号ID
    public category_id : number;
    private tab : string;
    private gid : any;
    public price : any;
    public money:any;
    private param : any = {};
    public category : any = {};
    public selectedPage : any;
    public selectedCategory : any;

    constructor(
        private HomeService : HomeService,
        private Pagination : Pagination,
        private router : Router,
        private route: ActivatedRoute,
        private userCenterService : userCenterService,
        private isB : isBindPNService
    ){
        let self : any = this;
        self.route.params.subscribe((params : any)=> {
            for (let key in params) {
                self.param[key] = params[key];
            }
        });
        if (self.param.page == undefined) {
            self.param.page = 1;
        }
        if (self.param.limit == undefined) {
            self.param.limit = 8;
        }
        self.getUserMsg();
        self.games.list = [];
        self.getGameList(self.param.page);
        //加载上拉加载
        self.pullLoading();
        self.getCategory();
        $.cookie("home","1");
    }
    // 用户信息
    private  getUserMsg(){
        let self : any = this;
        self.id = $.cookie("id");
        console.log(self.id);
        let data : any = {
            member_id: self.id,
        };
        self.userCenterService.userMsg({
            data: data,
            success: function (result : any) {
                self.money = result.money;
            }
        });
    }

    // 取消购买
    public noBuyGame(){
        this.isPayGame = true;
    }
    // 购买游戏1
    public buyGame(gameId:number,price:any){
        this.isPayGame = false;
        this.isB.isId();
        this.isB.isBindPN();
        this.gid = gameId;
        this.price = price;
    }
    // 购买游戏2
    public payGame(){
        let self: any = this;
        let mid: any = $.cookie("id");
        let data: any = {
            member_id : mid,
            game_id : self.gid
        };
        self.HomeService.payGame({
            data: data,
            success: function (result: any) {
                if (self.money <= 0 ){
                    this.router.navigate(['/user_recharge']);
                }else {
                    console.log(result);
                    alert("购买成功");
                    self.noBuyGame();
                    self.getUserMsg();
                }
            }
        })
    }
    //选择标签
    public seleteCategory(id:any){
        this.selectedCategory = id;
        this.category_id = id;
        this.getGameList();
    }
    // 移动端显示更多标签
    public tagShow(){
       /* let isHavetag = $(".nav-film ul").hasClass("tag_ul");
        if(isHavetag){
           //$(".nav-film ul").removeClass('tag_ul');
            $(".nav-film ul").removeClass('tag_ul');
            $("#more").html('收起');
            let tagHeight = $(".nav-film ul").height();
            $("#more").css("margin-top",tagHeight - 32);
        }else{
            $(".nav-film ul").addClass('tag_ul');
            $("#more").html('更多');
            $("#more").css("margin-top",16);
        }*/
       /*手风琴式*/

        let tagHeight = $(".tag_ul").height();
        if(tagHeight==64){
            $("#more").html('收起');
             $(".nav-film ul").animate(
                {height:'384px'},"7000"
            );
        }
        else{
            $("#more").html('更多');
            $(".nav-film ul").animate(
                {height:'64px'},"7000"
            );
        }
        //$(".tag_ul").slideToggle();
        /* let tagHeight = $(".tag_ul").height();
        if(tagHeight>64){
            $(".tag_ul").css("height","64px") ;
        }
        else {
            $(".tag_ul").css("height","auto").fadeIn(3000);
        }*/
    }
    /**
     * 公众号的分类列表
     *
     */
    public getCategory(){
        let self:any = this;
        let data:any = {};
        self.HomeService.getCategoryList({
            data:data,
            success: function (result:any) {
                if (result.error === 0){
                    self.category.list = result.data;
                }
            }
        })
    }

    /**
     * 公众号的游戏列表
     *
     * @param number: id 公众号ID
     * @param number: page 分页ID
     * @param number: limit 显示记录数
     */
    public getGameList() {
        let self : any = this;
        let data : any = {
            category_id: self.category_id,
            page: self.param.page,
            limit: self.param.limit
        };
        self.HomeService.getAllGameList({
            data: data,
            success: function (result : any) {
                self.games1.list = result.data;
                for( var i = 0; i < self.games1.list.length; i++){
                    self.games.list.push( self.games1.list[i] );

                }
                  /*for (let key in result.data) {
                      let val = result.data[key];
                      let data = {};
                      data['times'] = val['create_time'];
                      self.time.push(val['create_time']);
                      console.log(self.time);
                      //self.time = self.timeStamp2String(val['create_time']);
                      //self.time = Date.parse(val['create_time'].replace(/-/g, "/"));

                      //self.time = self.time.getTime();

                  }*/
                console.log(result);
                //self.games.list = result.data;
                self.games.count = result.count;
                self.resource_url = result.resource_url;
                self.games.pagination = self.Pagination.getPage(self.param.page,result.page_count);
                console.log(self.games.pagination);
                self.selectedPage = self.games.pagination.current;
            }
        });
    }

    public previousPage() {
        const current : number = parseInt(this.param.page);
        const page : number = this.Pagination.previousPage(current);
        this.param.page = page;
        this.selectedPage = page;
        this.router.navigate(['/home',{page:page}]);
        this.getGameList();
    }

    public nextPage() {
        const count : number = parseInt(this.games.pagination.count);
        const current : number = parseInt(this.param.page);
        const page : number = this.Pagination.nextPage(current, count);
        this.param.page = page;
        this.selectedPage = page;
        this.router.navigate(['/home',{page:page}]);
        this.getGameList();
    }

    public locationPage(page : number) {
        this.param.page = page;
        this.selectedPage = page;
        console.log(this.param.page);
        this.router.navigate(['/home',{page:page}]);
        this.getGameList();
    }
    public pullLoading() {
        var self:any = this;
        document.onscroll = function() {
            //if (document.body.scrollTop + document.body.clientHeight >= document.body.scrollHeight) {
            if ($(document).scrollTop() >= $(document).height() - $(window).height())  {
                //到达底部
                //$('.addMore').fadeIn(50);
                $(document).scrollTop();
                //alert("滚动条已经到达底部为" + $(document).scrollTop());
                self.loadingPart();

            }
        }
    }
    public loadingPart() {
        let self:any = this;
        self.param.page++;
        console.log(self.games.pagination.count);
        if( self.param.page > self.games.pagination.count ){
            $('#addMore').html('已经到底了').delay(3000).hide(50);
        }else {
            self.getGameList();
        }
    }
   public pageScroll(){
       var speed=200;//滑动的速度
       $('body,html').animate({ scrollTop: 0 }, speed);
       return false;
   }


}

